package com.lizhao.win007com.match.webapi.controller;

import com.lizhao.win007com.commons.pojo.info.model.*;
import com.lizhao.win007com.commons.pojo.match.model.Match;
import com.lizhao.win007com.commons.pojo.match.vo.MatchVO;
import com.lizhao.win007com.commons.pojo.match.vo.RelationInfoVO;
import com.lizhao.win007com.commons.restful.JsonPage;
import com.lizhao.win007com.commons.restful.JsonResult;
import com.lizhao.win007com.info.service.IInfoService;
import com.lizhao.win007com.match.service.IMatchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.poi.hssf.record.formula.functions.Int;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

@RestController
@Slf4j
@Api(tags = "match")
@RequestMapping("/match")
public class MatchController {
    @Autowired
    private IMatchService matchService;

    @DubboReference
    private IInfoService dubboInfoService;

    //通过表获取赛事信息
    @GetMapping("/list/matchtype/{matchtype:cup|league}/code/{code:one|two|three}/leagueid/{leagueid:[0-9]+}/page/{page:[0-9]+}/pagesize/{pagesize:[0-9]+}")
    @ApiOperation("根据开盘类型取数据")
    public JsonPage<Match> loadMatchInfoByCodeAndLeagueIdAndPageAndPageSize(@PathVariable String matchtype,@PathVariable String code,@PathVariable Integer leagueid,@PathVariable Integer page,@PathVariable Integer pagesize){
        String table = matchtype + "match-" + leagueid + "-" + code;
        int companyId = 545;
        return matchService.getMatchInfoByTableAndCompanyIdAndPageAndPageSize(table,companyId,page,pagesize);
    }

    //获取对阵数据
    @GetMapping("list/oppose/leagueid/{leagueid:[0-9]+}")
    @ApiOperation("获取对阵数据")
    public JsonResult<List<MatchVO>> loadOpposeInfoByLeagueId(@PathVariable Integer leagueid){
        int companyid = 545;
        List<MatchVO> list = matchService.getOpposeByLeagueIdAndCompanyId(leagueid, companyid);

        return JsonResult.ok("完成获取对阵数据", list);
    }

    //通过leagueid、code、season、companyid获取赛事数据
    @GetMapping("list/betlist/season/{season:[0-9]+\\-[0-9]+}")
    @ApiOperation("获取推荐列表")
    public JsonResult<List<RelationInfoVO>> loadBetList(@PathVariable String season){
        //通过leagueid、season、companyid获取赛事数据
        int companyid = 545;

        //获取开放的联赛赛事类型
        List<LeagueInfo> leagueInfo = dubboInfoService.getOpenLeagueInfo();
        List<RelationInfoVO> list = new ArrayList<>();

        for (LeagueInfo league : leagueInfo){
            Integer leagueid = league.getLeagueid();
            List<RelationInfoVO> tmpList = matchService.getMatchByLeagueIdAndSeasonAndCompanyId(leagueid, season, companyid);

            for (RelationInfoVO relationInfoVO : tmpList){
                list.add(relationInfoVO);
            }
        }
//        list = matchService.getMatchByLeagueIdAndSeasonAndCompanyId(8, season, companyid);
        return JsonResult.ok("完成获取推荐列表",list);
    }

    /**************************Excel 导出功能****************************/
    //按条件导出数据
//    @PostMapping("/export/odds")
//    @ApiOperation("按条件导出数据")
//    public JsonResult<Void> exportOddsOne(@RequestBody Match match){
//        if ("one".equals(match.getType())){
//            int leagueid = match.getLeagueid();
//            String season = match.getSeason();
//            String who = match.getWho();
//            BigDecimal odds = match.getOdds();
//
//            if (!"".equals(leagueid) && !"".equals(season) && !"".equals(who) && !"".equals(odds)){
//                matchService.getExportExcelList(leagueid,"one",545,season,who,odds);
//            }
//        }
//
//        return JsonResult.ok("导出成功！");
//    }
}
